<?php
require_once ('AbstractController.php');
include_once ("arc/ARC2.php");
/**
 * AuthorController.php
 *
 * @author		b.wenneker <b.wenneker@gmail.com>, 2010
 */
class PublisherController extends Thesis_AbstractController
{

	public function overviewAction ()
	{
		$store = $this->_getStore();
		
		// @todo: the query below retrieves authors, but we want publishers here.
		$query = '
			PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
			PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
			PREFIX dcterms: <http://purl.org/dc/terms/>
			
			SELECT ?name WHERE {
				?a rdf:type dcterms:publisher .
				?a rdfs:label ?name
			} ORDER BY ?name';
		
		
		$rows = $store->query($query, 'rows');
		$publisherCounts = array_fill_keys(range('a', 'z'), 0);
		foreach ((array) $rows as $row) {
			$name = lcfirst(ltrim($row['name']));
			$publisherCounts[$name[0]] += 1;
		}
		
		$this->view->assign('publisherCounts', $publisherCounts);
	}
	
	public function publishersAction ()
	{
		
		$character = $this->getRequest()->getParam('queryparam');
		
		if($this->_contentType == 'page'){
			$store = $this->_getStore();
			// @todo: the query below checks for authors, but we need publishers.
			$query =
			  'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
			  PREFIX dcterms: <http://purl.org/dc/terms/>
			  
			  SELECT DISTINCT ?name FROM <http://wisserver.st.ewi.tudelft.nl> WHERE {
				?a ?b dcterms:publisher .
				?a rdfs:label ?name
				FILTER regex(?name, "^' . $character . '", "i")
			  } ORDER BY ?name';
			
			$rows = $store->query($query, 'rows');
			$publishers = array();
			
			foreach ($rows as $row) {
				$publishers[] = $row['name'];
			}
			
			$this->view->assign('character', $character);
			$this->view->assign('publishers', $publishers);
		}else if($this->_contentType == 'data'){
			// User agent requests rdf data.
			
		}
	}
	
	public function indexAction ()
	{
		
		$publisher = $this->getRequest()->getParam('queryparam', 'no_publisher');
		if($this->_contentType == 'page'){
			$store = $this->_getStore();
			// @todo: the query below collects the titles of publications of a
			// certain author, we need the titles of publications published by
			// publisher with name $publisher.
			$query =
				'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
				PREFIX dcterms: <http://purl.org/dc/terms/>
				PREFIX dc: <http://purl.org/dc/elements/1.1/>
				
		  			SELECT ?title WHERE {
					?d dcterms:publisher <http://wisserver.st.ewi.tudelft.nl/resource/publisher/'.urlencode($publisher).'> .
					?d dc:title ?title .
		   		}';
			
			$rows = $store->query($query, 'rows');
			$titles = array();
			
			foreach ($rows as $row) {
				$titles[] = $row['title'];
			}
			
			$this->view->assign('publisher', $publisher);
			$this->view->assign('titles', $titles);
		}else if($this->_contentType == 'data'){
			// User agent requests rdf data.
			
		}
	}
}